package com.lab.net;

import java.awt.BorderLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.StringTokenizer;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Client {

	public static void main(String[] args) throws Exception {
		final String name = args[0];
		final JFrame frame = new JFrame("当前用户: " + name);
		LayoutManager m = new BorderLayout();
		frame.setSize(500, 300);

		frame.setLayout(m);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		final JTextArea jta = new JTextArea();

		JScrollPane jsp = new JScrollPane(jta);
		jta.setEditable(false);

		final JTextField jtf = new JTextField(20);
		JButton btn = new JButton("提交");
		final JComboBox jcb = new JComboBox();
		jcb.addItem("所有人");
		JPanel p = new JPanel();
		p.add(jcb);
		p.add(jtf);
		p.add(btn);
		frame.add(jsp);
		frame.add(p, "South");
		frame.setVisible(true);
		// =====================================================
		// 绑定服务器ip和端口
		final Socket client = new Socket("127.0.0.1", 9090);
		OutputStream os = client.getOutputStream();
		final PrintWriter out = new PrintWriter(os);
		out.println(args[0] + ":client");
		out.flush();
		// Font font = new Font("courier", 2, 15);
		class MyActionListener implements ActionListener {
			public void actionPerformed(ActionEvent e) {
				// 将输入信息以及输入菜单信息发送到服务器端
				if (jtf.getText().equals("")) {
					JOptionPane.showMessageDialog(frame, "输入内容不能为空!");
				} else {
					out.println(name + "*" + jcb.getSelectedItem() + "*"
							+ jtf.getText() + "*");
					out.flush();
					jtf.setText("");
				}
			}
		}
		ActionListener al = new MyActionListener();
		jtf.addActionListener(al);
		btn.addActionListener(al);

		BufferedReader in = new BufferedReader(new InputStreamReader(client
				.getInputStream()));
		// 读取服务器信息
		String session;
		String removeItem;
		while ((session = in.readLine()) != null) {
			if (session.contains(":client")) {
				StringTokenizer st = new StringTokenizer(session, ":");
				session = st.nextToken();
				// 添加到下拉菜单
				int count = 0;
				for (int i = 0; i < jcb.getItemCount(); i++) {
					if (session.equals(jcb.getItemAt(i))) {
						count++;
					}
				}
				if (count == 0) {
					jcb.addItem(session);
				}
			}// 接收欢迎语句
			else if (session.contains(":welcome")) {
				StringTokenizer st = new StringTokenizer(session, ":");
				session = st.nextToken();
				jta.append("欢迎" + session + "进入聊天室" + "\n");
			} else if (session.contains(":quit")) {
				StringTokenizer st = new StringTokenizer(session, ":");
				removeItem = st.nextToken();
				jcb.removeItem(removeItem);

			} else {
				// jta.setFont(font);
				jta.append(session + "\n");
			}
		}
	}
}
